package graph.drawings;

import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import graph.models.Graph;

public class GraphDrawing {
	// private Graph graph;
	// private Map<String, AbtractDrawing> shapes;

	// devide 2 variable for solve draw edge befere vertex
	private Map<String, VertexDrawing> vertexs;
	private Map<String, EdgeDrawing> edges;

	public GraphDrawing() {
		// shapes = new HashMap<String, AbtractDrawing>();
		vertexs = new HashMap<String, VertexDrawing>();
		edges = new HashMap<String, EdgeDrawing>();
	}

	public GraphDrawing(Graph graph) {
		// this.graph = graph;
		vertexs = new HashMap<String, VertexDrawing>();
		edges = new HashMap<String, EdgeDrawing>();
	}

	public void addShape(AbtractDrawing shape) {
		if (shape instanceof VertexDrawing) {
			vertexs.put(shape.getKey(), (VertexDrawing) shape);
		} else if (shape instanceof EdgeDrawing) {
			edges.put(shape.getKey(), (EdgeDrawing) shape);
		}

	}

	public void removeShape(String key) {

	}

	public VertexDrawing getVertex(String key) {
		return vertexs.get(key);
	}

	public void draw(Graphics2D graphics) {
		for (AbtractDrawing edge : edges.values()) {
			edge.draw(graphics);
		}

		for (AbtractDrawing vertex : vertexs.values()) {
			vertex.draw(graphics);
		}
	}

	public void UnSelectAll() {
		for (AbtractDrawing edge : edges.values()) {
			edge.select(false);
			;
		}

		for (VertexDrawing vertex : vertexs.values()) {
			vertex.select(false);
		}
	}

	public AbtractDrawing hitShape(Point point) {
		for (VertexDrawing vertex : vertexs.values()) {
			if (vertex.testHit(point)) {
				return vertex;
			}
		}

		for (EdgeDrawing edge : edges.values()) {
			if (edge.testHit(point)) {
				return edge;
			}
		}
		return null;
	}

	public Collection<VertexDrawing> getVertexs() {
		return vertexs.values();
	}

	public Collection<EdgeDrawing> getEdges() {
		return edges.values();
	}

	public void updateValueForEdge(String key, int value) {
		EdgeDrawing edge = edges.get(key);
		if (edge != null)
			edge.setValue(value);
	}
}
